package com.qf.merchant.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.common.page.PageInfo;
import com.qf.common.page.PageUtils;
import com.qf.common.response.Response;
import com.qf.entity.dto.Goods;
import com.qf.entity.dto.GoodsSku;
import com.qf.entity.dto.GoodsType;
import com.qf.entity.po.LoginMerchant;
import com.qf.service.GoodsService;
import com.qf.service.GoodsSkuService;
import com.qf.service.GoodsTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RequestMapping("goods")
@Controller
public class GoodsController {

    @Resource
    GoodsService goodsService;
    @Resource
    GoodsTypeService goodsTypeService;
    @Resource
    GoodsSkuService goodsSkuService;
    /**
     * 前往商品页面，携带商品类型数据和商品信息以及分页信息
     */
    @RequestMapping("list")
    public String toGoodsList(Model model, LoginMerchant loginMerchant, @RequestParam(value = "currentPage",required = false,defaultValue = "1") int currentPage){
        //获取商品列表并分页
        QueryWrapper<Goods> goodsWrapper = new QueryWrapper<>();
        goodsWrapper.lambda().eq(Goods::getMerchantId,loginMerchant.getId());
        PageInfo<Goods> pageInfo = PageUtils.getPageList(currentPage, goodsWrapper, goodsService);
        model.addAttribute("goodsList",pageInfo.getPageRecords());
        model.addAttribute("pageCounts",pageInfo.getPageCounts());
        return "goods/list";
    }

    /**
     * 根据商品id对其进行删除,相应套餐记录也要删除
     * @param goodsId
     * @return
     */
    @RequestMapping("del")
    @ResponseBody
    public Response<?> del(@RequestParam("id")Long goodsId){
        goodsService.removeByGoodsId(goodsId);
        return Response.success();
    }

    /**
     * 前往添加商品页面，根据商品id的值的不同返回不同的数据
     * @param model
     * @return
     */
    @RequestMapping("add")
    public String toGoodsAdd(Model model,@RequestParam(value = "id",required = false)Long goodsId){
        model.addAttribute("goodsTypeList",goodsTypeService.getAllByDesc());
        if (goodsId == null){
            return "goods/add";
        }
        model.addAttribute("goods",goodsService.getById(goodsId));
        model.addAttribute("skuList",goodsSkuService.getSkuListByGoodsId(goodsId));
        return "goods/add";
    }


    /**
     * 获取配置信息path
     */
    @Value("${upload.path}")
    String path;

    /**
     * 图片上传
     * @param file
     * @return
     */
    @RequestMapping("upload")
    @ResponseBody
    public Response<?> upload(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        String ext = originalFilename.substring(originalFilename.lastIndexOf("."));
        String fileName = String.format("%s%s",UUID.randomUUID().toString(),ext);
        File localFile = new File(path+fileName);
        file.transferTo(localFile);
        return Response.success("http://localhost:80/"+fileName);
    }

    @RequestMapping("save")
    @ResponseBody
    public Response<?> saveOrUpdateGoodsInfo(Goods goods, GoodsSku goodsSku,LoginMerchant loginMerchant){
        goodsService.saveOrUpdates(goods,goodsSku,loginMerchant);
        return  Response.success();
    }

}
